package com.kmproject.haifu.app.utils;

import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.irozon.library.HideKey;
import com.jess.arms.integration.AppManager;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DataHelper;
import com.jess.arms.widget.CustomPopupWindow;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.AppConstant;
import com.kmproject.haifu.app.hfinterface.PopuWindowSearchHistoryInterface;
import com.kmproject.haifu.mvp.model.entity.beans.HousePayWayBean;
import com.kmproject.haifu.mvp.model.entity.beans.UserBean;
import com.kmproject.haifu.mvp.model.entity.resbean.PopuWindowSelectSearchBean;
import com.kmproject.haifu.mvp.ui.activity.SecondHandListShowActivity;
import com.kmproject.haifu.mvp.ui.adapter.PopuwindowSearchNewAdapter;
import com.kmproject.haifu.mvp.ui.holder.PopuwindowSecondHandSearchViewHold;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class PopuwindowContralUtils {
    private static volatile PopuwindowContralUtils popuwindowContralUtils;
    List<PopuWindowSelectSearchBean> popuWindowSelectSearchBeans;
    private PopuwindowContralUtils() {
    }

    public static PopuwindowContralUtils getPopuwindowContralUtils() {
        if (popuwindowContralUtils == null) {
            synchronized (PopuwindowContralUtils.class) {
                if (popuwindowContralUtils == null) {
                    popuwindowContralUtils = new PopuwindowContralUtils();
                }
            }
        }
        return popuwindowContralUtils;
    }

    public PopuwindowContralUtils init() {
        return popuwindowContralUtils;
    }

    /**
     * 控制popu 搜索控制
     */
    public void showPopuWindowToSearch(Activity activity, View view, PopuWindowSearchHistoryInterface popuWindowSearchHistoryInterface) {
        HideKey.initialize(activity, view);
        CustomPopupWindow customPopupWindow = new CustomPopupWindow.Builder().contentView(CustomPopupWindow.inflateView(activity, R.layout.popuwindow_secondhand_selectsearchaddress)).parentView(view)
                .animationStyle(R.style.mypopwindow_anim_style).customListener(new CustomPopupWindow.CustomPopupWindowListener() {
                    @Override
                    public void initPopupView(View contentView) {

                    }
                }).isHaveBackGround(false).build();
        View view1 = customPopupWindow.getContentView();
        PopuwindowSecondHandSearchViewHold popuwindowViewHold = new PopuwindowSecondHandSearchViewHold(view1);
         popuWindowSelectSearchBeans = DataHelper.getDeviceData(activity, AppConstant.sendhand_Search_history);
        popuwindowViewHold.popuwindowSelectsearchaddressRecyclerView1.setLayoutManager(new LinearLayoutManager(activity));
        PopuwindowSearchNewAdapter popuwindowSearchNewAdapter;
        if (popuWindowSelectSearchBeans == null || popuWindowSelectSearchBeans.isEmpty()) {
            popuwindowSearchNewAdapter = new PopuwindowSearchNewAdapter(null);
            popuwindowViewHold.popuwindowSelectsearchaddressRecyclerView1.setAdapter(popuwindowSearchNewAdapter);
            popuwindowSearchNewAdapter.setEmptyView(AppUtil.setEmptyView(activity, "没有搜索记录", "", null, true));
        } else {
            popuwindowSearchNewAdapter = new PopuwindowSearchNewAdapter(popuWindowSelectSearchBeans);
            popuwindowViewHold.popuwindowSelectsearchaddressRecyclerView1.setAdapter(popuwindowSearchNewAdapter);
        }
        popuwindowSearchNewAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        popuwindowViewHold.popuwindowSelectsearchaddressXext.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s == null || s.toString().trim().length() < 1) {
                    //输入内容小于1 的时候显示第一个搜索
                    popuwindowViewHold.popuwindowSelectsearchaddressCancel.setText("取消");
                } else if (s.toString().trim().length() > 0) {
                    //s.toString().trim().length()<2避免多次刷新同样的数据
                    //有搜索内容的时候显示第二个搜索
                    popuwindowViewHold.popuwindowSelectsearchaddressCancel.setText("确定");
                }
            }
        });
        popuwindowSearchNewAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                popuWindowSearchHistoryInterface.callBack(null, popuwindowSearchNewAdapter.getItem(position));
                customPopupWindow.dismiss();
            }
        });
        popuwindowViewHold.popuwindowSelectsearchaddressCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popuwindowViewHold.popuwindowSelectsearchaddressCancel.getText().toString().equals("确定")) {
                    popuWindowSearchHistoryInterface.callBack(popuwindowViewHold.popuwindowSelectsearchaddressXext.getText().toString(), null);
                    if (popuWindowSelectSearchBeans==null)
                    {
                        popuWindowSelectSearchBeans=new ArrayList<>();
                    }
                    if (popuWindowSelectSearchBeans.size()>20)
                    {
                        popuWindowSelectSearchBeans.subList(0,19);
                    }
                    UserBean userBean=DataHelper.getDeviceData(activity,AppConstant.USER_INFO);
                    popuWindowSelectSearchBeans.add(0,new PopuWindowSelectSearchBean(TimeUtils.date2String(new Date()),popuwindowViewHold.popuwindowSelectsearchaddressXext.getText().toString(),userBean!=null?userBean.getMobile():"",null));
                    DataHelper.saveDeviceData(activity,AppConstant.sendhand_Search_history,popuWindowSelectSearchBeans);

                }
                customPopupWindow.dismiss();
            }
        });
        popuwindowViewHold.popuwindowSelectsearchaddressClearAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DataHelper.saveDeviceData(activity,AppConstant.sendhand_Search_history,null);
                ArmsUtils.makeText(activity,"清除成功");
                if (popuwindowSearchNewAdapter!=null)
                {
                    popuwindowViewHold.popuwindowSelectsearchaddressRecyclerView1.setAdapter(popuwindowSearchNewAdapter);
                    popuwindowSearchNewAdapter.setEmptyView(AppUtil.setEmptyView(activity, "没有搜索记录", "", null, true));

                }

            }
        });
        customPopupWindow.show();
    }


}
